<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

class LettermanModelNewsletters extends JModel
{

	var $_data = null;

	var $_total = null;
        
        var $_pagination = null;
        
        var $_campaigns = null;
        
        var $published = null;
        
        var $ordering = null;

	function __construct()
	{
		parent::__construct();

	}

	function getData()
	{
		if (empty($this->_data))
		{
			$query = $this->_buildQuery();
			$this->_data = $this->_getList($query);
		}
		return $this->_data;
	}
        
        function _buildWhere ()
        {

            $state = JRequest::getInt('state', 3);
            $campaign = JRequest::getInt('campaign','');
            
            $where = array();
            
            if ($campaign >= 1)
            {
                $where[] = "n.campaign = $campaign" ;
            }

            if ($state < 3)
            {
                $where[] = "n.published = $state" ;
            }
            
            $where[] = "n.campaign = c.id";
            
            $where = ( count( $where ) ? ' WHERE '. implode( ' AND ', $where ) : '' );
            return $where;

        }
        
	function _buildOrder()
	{
		global $mainframe, $option;

		$filter_order		= $mainframe->getUserStateFromRequest( $option.'filter_order','filter_order','n.created_time','cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.'filter_order_Dir','filter_order_Dir','','word' );

		if ($filter_order == 'n.created_time'){
			$order	= ' ORDER BY n.created_time '.$filter_order_Dir;
		} else {
			$order 	= ' ORDER BY '.$filter_order.' '.$filter_order_Dir.' , n.created_time ';
		}

		return $order;
	}        

	function getTotal()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}

	function _buildQuery()
	{

                $where = $this->_buildWhere();
                        
                $query = "SELECT n.id, "
                        . "n.subject, "
                        . "n.published, "
                        . "n.checked_out, "
                        . "n.publish_up, "
                        . "n.publish_down, "
                        . "n.sent, "
                        . "n.hits, "
                        . "c.title AS campaign_title "
                        . "FROM jos_letterman_campaigns AS c, "
                        . "jos_letterman AS n "
                        . $this->_buildWhere()
                        . $this->_buildOrder();

		return $query;
	}
        
	function getPagination()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}

		return $this->_pagination;
	}
        
        function getCampaigns()
        {
            $query = "SELECT id AS value, "
                    ."title AS text "
                    ."FROM #__letterman_campaigns "
                    ."ORDER BY title";
            $query = $this->_buildQuery();
	    $campaigns = $this->_getList($query);
            
            return $this->_campaigns;
        }
}
?>